package com.compass.util;

import com.compass.vo.LoginUser;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * Created with IntelliJ IDEA.
 * User: li
 * Date: 2019/3/24
 * Time: 16:09
 * PROJECT：compass
 * PACKAGE: com.compass
 *
 * @author:55324936@qq.com To change this template use File | Settings | File Templates.
 */
@Component
public class SessionUtil {

    /**
     * 获取session里面的用户名
     * **/
    public static String getUserName(){
       LoginUser loginUser= getSessionUser();
       return loginUser.getUserName();
    }
    /**
     * 获取session里面的用户id
     * **/
    public static Integer getUserId(){
        LoginUser loginUser= getSessionUser();
        return loginUser.getId();
    }
    /**
     * 获取session里面的用户
     * **/
    public static LoginUser getSessionUser(){
      return  (LoginUser)getSession().getAttribute("loginUser");
    }
    /**
     * 获取session信息
     * **/
    public static HttpSession getSession(){
       return getRequest().getSession(false);
    }
    /**
     * 获取request信息
     * **/
    public static HttpServletRequest getRequest(){
      ServletRequestAttributes requestAttributes =(ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
      return requestAttributes==null ? null: requestAttributes.getRequest();
    }

}
